package controller.web;

import controller.web.Servlet;
import business.DaftarUser;
import business.User;
import business.Admin;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RegisterServlet extends Servlet {

    public RegisterServlet(){
        super();
        setTemplate("/WEB-INF/registrasi.jsp");
    }

    @Override
    protected void process() {

        setMessage("");

        if (getRequest().getParameter("act") != null && getRequest().getParameter("act").equals("add")) {
            try {
                if (validate_field()) {
                        DaftarUser du = new DaftarUser();
                        String stats = getRequest().getParameter("tp");
                        int stat = Integer.parseInt(stats);
                        {if (stat == 1) {
                         {
                            User u = new User();
                            u.setUsername(getRequest().getParameter("username"));
                            u.setPassword(getRequest().getParameter("password"));
                            u.setEmail(getRequest().getParameter("email"));
                            u.setNama_depan(getRequest().getParameter("nama_depan"));
                            u.setNama_belakang(getRequest().getParameter("nama_belakang"));
                            u.settelepon(getRequest().getParameter("telepon"));
                            u.setalamat(getRequest().getParameter("alamat"));
                            u.setkota(getRequest().getParameter("kota"));
                            u.setprovinsi(getRequest().getParameter("provinsi"));
                            u.setnegara(getRequest().getParameter("negara"));
                            u.setKodePos(getRequest().getParameter("KodePos"));
                            u.setEmail(getRequest().getParameter("email"));
                            du.tambahUser(u);
                            }
                         }
                        if (stat == 0) {
                        Admin o = du.getAdmin(getRequest().getParameter("username"));
                        if (o == null) {
                            o = new Admin();
                            o.setUsername(getRequest().getParameter("username"));
                            o.setPassword(getRequest().getParameter("password"));
                            du.tambahAdmin(o);
                                    }
                            }
                        
                        }
                        }
                getResponse().sendRedirect("userHome");
            } catch (IOException ex) {
                Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
            }



        }
    }

    boolean validate_field() {

        String username = getRequest().getParameter("username");
        String password = getRequest().getParameter("password");
        String nama_depan = getRequest().getParameter("nama_depan");
        String nama_belakang = getRequest().getParameter("nama_belakang");
        String telepon = getRequest().getParameter("telepon");
        String alamat = getRequest().getParameter("alamat");
        String kota = getRequest().getParameter("kota");
        String provinsi = getRequest().getParameter("provinsi");
        String negara = getRequest().getParameter("negara");
        String KodePos = getRequest().getParameter("KodePos");
        String email = getRequest().getParameter("email");
        if(username == null||password == null||alamat == null||telepon == null||email == null){
            return false;
        /*}
        if(username.trim().equals("")||password.trim().equals("")||address.trim().equals("")||handphone.trim().equals("")||email.trim().equals("")){
            return false;*/
        }

        return true;
    }


}
